<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:sgjl:coreClsgjl:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="primary" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="primary" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.clsgjlCph }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddSgjlCoreClsgjlList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { coreClsgjlDelete, coreClsgjlListData } from '/@/api/cardd/sgjl/coreClsgjl';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('cardd.sgjl.coreClsgjl');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('车辆事故记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('车牌号'),
        field: 'clsgjlCph',
        component: 'Input',
      },
      {
        label: t('品牌型号'),
        field: 'clsgjlPpxh',
        component: 'Input',
      },
      {
        label: t('事故发生日期'),
        field: 'clsgjlSgfsrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('驾驶员'),
        field: 'clsgjlJsy',
        component: 'Input',
      },
      {
        label: t('事故发生地点'),
        field: 'clsgjlSgfsdd',
        component: 'Input',
      },
      {
        label: t('事故性质'),
        field: 'clsgjlSgxz',
        component: 'Input',
      },
      {
        label: t('事故责任方'),
        field: 'clsgjlSgzrf',
        component: 'Input',
      },
      {
        label: t('车损情况'),
        field: 'clsgjlCsqk',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('车牌号'),
      dataIndex: 'clsgjlCph',
      key: 'a.clsgjl_cph',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('品牌型号'),
      dataIndex: 'clsgjlPpxh',
      key: 'a.clsgjl_ppxh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆识别代码'),
      dataIndex: 'clsgjlClsbdm',
      key: 'a.clsgjl_clsbdm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发动机号码'),
      dataIndex: 'clsgjlFdjhm',
      key: 'a.clsgjl_fdjhm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('事故发生日期'),
      dataIndex: 'clsgjlSgfsrq',
      key: 'a.clsgjl_sgfsrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'clsgjlJsy',
      key: 'a.clsgjl_jsy',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('事故发生地点'),
      dataIndex: 'clsgjlSgfsdd',
      key: 'a.clsgjl_sgfsdd',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('事故性质'),
      dataIndex: 'clsgjlSgxz',
      key: 'a.clsgjl_sgxz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('事故责任方'),
      dataIndex: 'clsgjlSgzrf',
      key: 'a.clsgjl_sgzrf',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车损情况'),
      dataIndex: 'clsgjlCsqk',
      key: 'a.clsgjl_csqk',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建人'),
      dataIndex: 'clsgjlCjrName',
      key: 'a.clsgjl_cjr_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('修改人'),
      dataIndex: 'updateByName',
      key: 'a.update_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑车辆事故记录'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:sgjl:coreClsgjl:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除车辆事故记录'),
        popConfirm: {
          title: t('是否确认删除车辆事故记录'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:sgjl:coreClsgjl:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: coreClsgjlListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/cardd/sgjl/coreClsgjl/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await coreClsgjlDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
